// 🎯 ES6模块：移动端全屏功能
// 存储原始样式的变量
let originalStyles = {};

// 定义需要管理的样式属性
const STYLE_PROPERTIES = {
  styles: [
    "position",
    "top",
    "left",
    "width",
    "height",
    "transform",
    "transformOrigin",
    "zIndex",
  ],
  attributes: ["width", "height"], // DOM属性，非CSS样式
};

// 统一的样式保存函数
function saveElementStyles(element, prefix) {
  if (!element) return;

  // 保存CSS样式
  STYLE_PROPERTIES.styles.forEach((prop) => {
    originalStyles[prefix + prop] = element.style[prop];
  });

  // 保存DOM属性（仅对canvas和video元素有效）
  STYLE_PROPERTIES.attributes.forEach((attr) => {
    if (element[attr] !== undefined) {
      originalStyles[prefix + attr + "Value"] = element[attr];
    }
  });
}

// 统一的样式恢复函数
function restoreElementStyles(element, prefix) {
  if (!element) return;

  // 恢复CSS样式
  STYLE_PROPERTIES.styles.forEach((prop) => {
    element.style[prop] = originalStyles[prefix + prop] || "";
  });

  // 恢复DOM属性
  STYLE_PROPERTIES.attributes.forEach((attr) => {
    if (
      element[attr] !== undefined &&
      originalStyles[prefix + attr + "Value"] !== undefined
    ) {
      element[attr] = originalStyles[prefix + attr + "Value"];
    }
  });
}

// 保存所有元素的原始样式
function saveAllOriginalStyles() {
  const containerWrap = document.getElementById("video-container-wrap");
  const canvas = document.getElementById("video-containercanvas_draw0");
  const player = document.getElementById("video-container-player");
  const container = document.getElementById("video-container");

  // 统一保存所有元素的样式
  saveElementStyles(containerWrap, "videoContainerWrap");
  saveElementStyles(canvas, "videoContainerCanvasDraw0");
  saveElementStyles(player, "videoContainerPlayer");
  saveElementStyles(container, "videoContainer");
}

// 设置全屏样式的函数
function setFullScreenStyles() {
  const containerWrap = document.getElementById("video-container-wrap");
  const canvas = document.getElementById("video-containercanvas_draw0");
  const player = document.getElementById("video-container-player");
  const container = document.getElementById("video-container");

  // 设置容器外层样式
  if (containerWrap) {
    containerWrap.style.transform = "rotate(90deg)";
    containerWrap.style.width = "100vh";
    containerWrap.style.height = "100vw";
    containerWrap.style.transformOrigin = `${window.innerWidth / 2}px ${
      window.innerWidth / 2
    }px`;
    containerWrap.style.position = "fixed";
    containerWrap.style.top = "0";
    containerWrap.style.left = "0";
    containerWrap.style.zIndex = "1000";
  }

  // 设置 canvas 样式
  if (canvas) {
    canvas.style.position = "fixed";
    canvas.style.top = "0";
    canvas.style.left = "0";
    canvas.width = window.innerHeight;
    canvas.height = window.innerWidth - 32;
    canvas.style.width = window.innerHeight + "px";
    canvas.style.height = window.innerWidth - 32 + "px";
  }

  // 设置 player 样式
  if (player) {
    player.style.position = "fixed";
    player.width = window.innerHeight;
    player.height = window.innerWidth - 32;
    player.style.width = window.innerHeight + "px";
    player.style.height = window.innerWidth - 32 + "px";
  }

  // 设置 container 样式
  if (container) {
    container.style.width = window.innerHeight + "px";
    container.style.height = window.innerWidth - 32 + "px";
  }
}

// 恢复所有元素的原始样式
function restoreAllOriginalStyles() {
  const containerWrap = document.getElementById("video-container-wrap");
  const canvas = document.getElementById("video-containercanvas_draw0");
  const player = document.getElementById("video-container-player");
  const container = document.getElementById("video-container");

  // 统一恢复所有元素的样式
  restoreElementStyles(containerWrap, "videoContainerWrap");
  restoreElementStyles(canvas, "videoContainerCanvasDraw0");
  restoreElementStyles(player, "videoContainerPlayer");
  restoreElementStyles(container, "videoContainer");
}

// 🎯 清理样式缓存（可选的工具函数）
function clearStylesCache() {
  originalStyles = {};
}

// 默认导出主要API
// export default {
//   saveAllOriginalStyles,
//   setFullScreenStyles,
//   restoreAllOriginalStyles,
//   clearStylesCache,
// };

window.saveAllOriginalStyles = saveAllOriginalStyles;
window.setFullScreenStyles = setFullScreenStyles;
window.restoreAllOriginalStyles = restoreAllOriginalStyles;
window.clearStylesCache = clearStylesCache;
